//
//  TrayDropButton.m
//  SohoNaru
//
//  Created by pnpWhite on 13. 7. 9..
//  Copyright (c) 2013년 ladaka Inc. All rights reserved.
//

#import "TrayDropButton.h"
#import <QuartzCore/QuartzCore.h>
#import "WUtil.h"

@implementation TrayDropButton
@synthesize imgPath = _imgPath;
@synthesize btnTag = _btnTag;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TrayDropButton" owner:self options:nil];
        self = [nib objectAtIndex:0];
    }
    return self;
}

- (id) initWithFrame:(CGRect)frame thumbNail:(NSString *)imgPath {
    self = [super initWithFrame:frame];
    
    if (self) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TrayDropButton" owner:self options:nil];
        self = [nib objectAtIndex:0];
        
        _isScrollEnable = YES;
        self.imgPath = imgPath;
    }
    
    return self;
}



- (void) awakeFromNib {
    _isScrollEnable = YES;
}


- (void) setImgPath:(NSString *)imgPath {
    
    _imgPath = imgPath;
    
    UIImage *img = [UIImage imageWithContentsOfFile:_imgPath];
    
//    if(img.size.width > 640)
//        img = [WUtil resizeWithImage:img scaledToSize:CGSizeMake(img.size.width/3, img.size.height/3)];
    
//    if (img.size.height > img.size.width) {
//        img = [WUtil resizeWithImage:img scaledToSize:CGSizeMake(240, 320)];
//    }
//    else {
//        img = [WUtil resizeWithImage:img scaledToSize:CGSizeMake(320, 240)];
//    }
    
    img = [WUtil resizeWithImage:img scaledToSize:CGSizeMake(120, 120)];
    NSLog(@"imgSize:%f, %f", img.size.width, img.size.height);
    self.ivImg.image = img;
    self.ivDrop.image = img;
}

- (NSString *) imgPath {
    
    return _imgPath;
}


- (void) setBtnTag:(NSInteger)btnTag {
    _btnTag = btnTag;
}

- (NSInteger) btnTag {
    return _btnTag;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/



- (IBAction) onClickDelete:(id)sender {
//    UIButton *btn = (UIButton *)sender;
//    self.tag = btn.tag;
    NSLog(@"tag:%i", self.btnTag);
    
    [self removeFromSuperview];
    if ([self.delegate respondsToSelector:@selector(didImageDelete:)]) {
        [self.delegate didImageDelete:self];
    }
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    
    if (_isScrollEnable != NO) {
        _originalPoint = self.ivDrop.center;
        NSLog(@"originalP:%f, %f", _originalPoint.x, _originalPoint.y);
        
        self.svParent.scrollEnabled = NO;
        
        self.ivDrop.hidden = NO;
        self.ivDrop.alpha = 0.7;
        
        CGPoint newPoint = CGPointZero;
        newPoint = [self.mainView convertPoint:[[touches anyObject] locationInView:self.mainView] toView:self.mainView];
        NSLog(@"point : x=%f, y=%f", newPoint.x, newPoint.y);
        
        _originalOutsidePoint = newPoint;
        
        self.ivDrop.center = newPoint;
        self.layer.borderColor = UIColorFromHexToRGB(0xD2202F).CGColor;
        self.layer.borderWidth = 2;
        
        [self.mainView addSubview:self.ivDrop];
        [self.mainView bringSubviewToFront:self.ivDrop];
        
        _isScrollEnable = NO;
    }
}


- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];
    
    [UIView beginAnimations:@"dropStart" context:nil];
    [UIView setAnimationDuration:.001];
    [UIView setAnimationBeginsFromCurrentState:YES];
    
    UITouch *touch = [touches anyObject];
    self.ivDrop.center = [touch locationInView:self.mainView];
    
    [UIView commitAnimations];
    
    if ([self.delegate respondsToSelector:@selector(didViewInside:imgPath:touching:)]) {
        [self.delegate didViewInside:self.ivDrop imgPath:self.imgPath touching:NO];
    }
}


- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    
    self.ivDrop.hidden = YES;
    [self bringSubviewToFront:self.mainView];
    
    self.layer.borderWidth = 0;
    
    [UIView beginAnimations:@"dropEnd" context:nil];
    [UIView setAnimationDuration:0.4];
    [UIView setAnimationBeginsFromCurrentState:YES];
    
    
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    [UIView commitAnimations];
    self.svParent.scrollEnabled = YES;
    
    if ([self.delegate respondsToSelector:@selector(didViewInside:imgPath:touching:)]) {
        [self.delegate didViewInside:self.ivDrop imgPath:self.imgPath touching:YES];
    }
    self.ivDrop.center = _originalPoint;
}

-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    
    if ([animationID isEqualToString:@"dropEnd"] && finished) {
        
        //        self.ivDropImg.center = _originalPoint;
        
        [self.svParent addSubview:self.ivDrop];
        _isScrollEnable = YES;
    }
}
@end
